cd "WORKING DIRECTORY"

//CENSUS All Sectors: County Business Patterns, including ZIP Code Business Patterns, by Legal Form of Organization and Employment Size Class for the U.S., States, and Selected Geographies: 2021
import delimited "CBP2021.CB2100CBP-Data_row_2_removed_for_import.csv", clear
gen zip=substr(name,5,5)
destring zip, replace
//Keep only rows with all establishment data (as opposed to separate rows for biz w/5 or fewer employees, etc)
keep if empszes_label=="All establishments"
destring emp, replace force

preserve
//Keep only retail
keep if naics2017_label=="Retail trade"
rename estab retail_estab
keep zip retail_estab
save "retail_estab.dta", replace
restore
preserve
//Keep only health
keep if naics2017_label=="Health care and social assistance"
rename estab health_estab
keep zip health_estab
save "health_estab.dta", replace
restore
//Keep only ALL
keep if naics2017_label=="Total for all sectors"
keep zip emp 
merge 1:1 zip using retail_estab.dta
drop _merge
merge 1:1 zip using health_estab.dta
drop _merge
replace retail_estab=0 if retail_estab==.
replace health_estab=0 if health_estab==.
gen retail=retail_estab/100
gen health=health_estab/100
replace emp = emp/1000
label var emp "Employees (1,000s)"
label var retail "Retail Establishments (100s)"
label var health "Health Establishments (100s)"
keep emp health retail zip
save "employment_zip.dta", replace

use "cccs_2023_data.dta", replace
//change directory to subfolder where tables/figures will be saved
cd "tables"

***RESTRICT SAMPLE (drop 3 incomplete respondents)
reg NIMBYAbortion NIMBYHomeless N_abortion_trt race_* age educ income income_mis i.gender i.rent_own i.abortion 
keep if e(sample)

**STANDARDIZE OUTCOME VARIABLES
qui sum NIMBYAbortion 
replace NIMBYAbortion = (NIMBYAbortion-r(mean))/r(sd)
qui sum NIMBYHomeless 
replace NIMBYHomeless = (NIMBYHomeless-r(mean))/r(sd)

**INDICATORS USED IN MODELS BELOW
tab gender, gen(g_)
rename g_1 g_woman
rename g_2 g_man
rename g_3 g_other
label var g_woman "Woman (1=yes)"
label var g_man "Man (1=yes)"
label var g_other "Other Gender (1=yes)"

tab rent_own, gen(home_)
rename home_1 home_rent
rename home_2 home_mortgage
rename home_3 home_own
rename home_4 home_other
label var home_rent "Renter (1=yes)"
label var home_mortgage "Pay Mortgage (1=yes)"
label var home_own "Own Outright (1=yes)"
label var home_other "Other Arrangement (1=yes)"

tab abortion, gen(ab_)
rename ab_1 ab_prolife
rename ab_2 ab_mixed
rename ab_3 ab_prochoice
label var ab_prolife "Abortion: Pro-life (1=yes; pre-treat)"
label var ab_mixed "Abortion: Mixed (1=yes; pre-treat)"
label var ab_prochoice "Abortion: Pro-choice (1=yes; pre-treat)"


**INTERACTIONS OF INTEREST
//Homeownership
gen homeless_trtXhomeowner=homeowner*N_homeless_trt
gen abortion_trtXhomeowner=homeowner*N_abortion_trt
gen homeless_trtXrenter=renter*N_homeless_trt
gen abortion_trtXrenter=renter*N_abortion_trt
label var homeless_trtXhomeowner "Your Neighborhood x Homeowner"
label var abortion_trtXhomeowner "Your Neighborhood x Homeowner"
//Abortion attitudes
gen prolifeXtrt=N_abortion_trt*ab_prolife
gen mixedXtrt=N_abortion_trt*ab_mixed
gen prochoiceXtrt=N_abortion_trt*ab_prochoice
label var prolifeXtrt "Your Neighborhood x Pro-life"
label var mixedXtrt "Your Neighborhood x Abortion/Mixed"
label var prochoiceXtrt "Your Neighborhood x Pro-choice"

//Demographics for models controling for demographic x treatment interactions
foreach i of varlist race_* age educ income income_mis g_man g_other {
	gen X`i'Xa_trt=N_abortion_trt*`i'
	gen X`i'Xh_trt=N_homeless_trt*`i'
}

**SUMMARY STATISTICS (TABLE SM.1)
estpost summarize NIMBYAbortion NIMBYHomeless home_mortgage home_own home_other ab_prolife ab_mixed ab_prochoice race_* age educ income income_mis g_man g_other 
eststo all, title("Full Sample")
estpost summarize NIMBYAbortion NIMBYHomeless home_mortgage home_own home_other ab_prolife ab_mixed ab_prochoice race_* age educ income income_mis g_man g_other if N_abortion_trt==0
eststo illinois, title("Illinois")
estpost summarize NIMBYAbortion NIMBYHomeless home_mortgage home_own home_other ab_prolife ab_mixed ab_prochoice race_* age educ income income_mis g_man g_other  if N_abortion_trt==1
eststo neighborhood, title("Neighborhood")
esttab all illinois neighborhood using "SM_1_sumstats.tex", cells(mean(fmt(a2))) replace title(Summary Statistics \label{sumstats}) addnote(Cell entries are means.) nogaps booktabs wrap varwidth(40) b(%9.3f) se(%9.3f) star(+ 0.10 * 0.05 ** .01) nonotes nodepvars label collabel(none)

//BALANCE TEST
foreach i of varlist home_mortgage home_own home_other ab_prolife ab_mixed ab_prochoice race_* age educ income income_mis g_man g_other {
	qui ttest `i', by(N_abortion_trt)
	display r(p)
}

***DIRECT EFFECTS MODELS
reg NIMBYAbortion N_abortion_trt race_* age educ income income_mis g_man g_other home_mortgage home_own home_other ab_mixed ab_prochoice, r
estimates store abortion_pooled
reg NIMBYHomeless N_homeless_trt race_* age educ income income_mis g_man g_other home_mortgage home_own home_other, r
estimates store homeless_pooled
sum NIMBYHomeless if N_homeless_trt==0

***HOMEOWNERSHIP AND ABORTOIN ATTITUDE INTERACTIONS****
//placeholder treatment and trtXhomeowner vars so NIMBY labels/coefficients aligned in Tables across experiments
gen TREAT = N_abortion_trt 
label var TREAT "...in the neighborhood where you live."
gen TREATXHomeowner = abortion_trtXhomeowner 
label var TREATXHomeowner "Your Neighborhood x Homeowner"
label var NIMBYAbortion "Out-of-State Abortion"
label var NIMBYHomeless "Homeless Shelters"
//reestimating direct effect model (identical to above) but w/placeholder "TREAT" variable so suitable for dumping out to regression table
reg NIMBYAbortion TREAT race_* age educ income income_mis g_man g_other home_mortgage home_own home_other ab_mixed ab_prochoice, r
estimates store abortion_pooled_reg

*ABORTION INTERACTIONS
//EST BY ABORTION ATTS
reg NIMBYAbortion N_abortion_trt mixedXtrt prochoiceXtrt race_* age educ income income_mis g_man g_other home_mortgage home_own home_other  ab_mixed ab_prochoice, r
estimates store abortion_prolife
//for regression output
reg NIMBYAbortion TREAT mixedXtrt prochoiceXtrt race_* age educ income income_mis g_man g_other home_mortgage home_own home_other  ab_mixed ab_prochoice, r
estimates store abortion_prolife_reg
reg NIMBYAbortion N_abortion_trt prolifeXtrt prochoiceXtrt race_* age educ income income_mis g_man g_other home_mortgage home_own home_other ab_prolife ab_prochoice, r
estimates store abortion_mixed
reg NIMBYAbortion N_abortion_trt prolifeXtrt mixedXtrt race_* age educ income income_mis g_man g_other home_mortgage home_own home_other  ab_prolife ab_mixed, r
estimates store abortion_prochoice
//EST FOR RENTERS
reg NIMBYAbortion N_abortion_trt TREATXHomeowner race_* age educ income income_mis g_man g_other home_mortgage home_own home_other ab_mixed ab_prochoice, r 
estimates store abortion_renters
//EST FOR RENTERS (FOR REGRESSION OUTPUT)
reg NIMBYAbortion TREAT TREATXHomeowner race_* age educ income income_mis g_man g_other home_mortgage home_own home_other ab_mixed ab_prochoice, r 
estimates store abortion_renters_reg
//EST FOR OWNERS
reg NIMBYAbortion N_abortion_trt abortion_trtXrenter race_* age educ income income_mis g_man g_other home_mortgage home_own home_other ab_mixed ab_prochoice, r
estimates store abortion_owners
//BOTH ABORTION ATTITUDE AND HOMEOWNERSHIP INTERACTED, plus demographic x treatment interactions
reg NIMBYAbortion TREAT race_* age educ income income_mis g_man g_other *Xa_trt mixedXtrt prochoiceXtrt TREATXHomeowner home_mortgage home_own home_other ab_mixed ab_prochoice, r
estimates store abortion_interact

//Swap placeholder treatment to Homelessness experiments (so, now estimating effects from Homeless Shelter experiment, using TREAT as variable of interest, but replacing that variable with the homeless shelter treatment)
replace TREAT = N_homeless_trt 
replace TREATXHomeowner = homeless_trtXhomeowner 
reg NIMBYHomeless TREAT race_* age educ income income_mis g_man g_other home_mortgage home_own home_other, r
estimates store homeless_pooled_reg
*HOMELESS INTERACTIONS
//EST FOR RENTERS
reg NIMBYHomeless N_homeless_trt TREATXHomeowner race_* age educ income income_mis g_man g_other home_mortgage home_own home_other, r 
estimates store homeless_renters
//EST FOR RENTERS (FOR REGRESSION OUTPUT)
reg NIMBYHomeless TREAT TREATXHomeowner race_* age educ income income_mis g_man g_other home_mortgage home_own home_other, r 
estimates store homeless_renters_reg
//EST FOR OWNERS
reg NIMBYHomeless N_homeless_trt homeless_trtXrenter race_* age educ income income_mis g_man g_other home_mortgage home_own home_other, r
estimates store homeless_owners
//FULLY INTERACTED (i.e., includes demographic x treatment interactions)
reg NIMBYHomeless TREAT race_* age educ income income_mis g_man g_other *Xh_trt TREATXHomeowner home_mortgage home_own home_other, r 
estimates store homeless_interact

**PLOT COEFFICIENTS OF INTEREST FOR FIGURE 1
coefplot (homeless_pooled, aseq("All") \ abortion_pooled, aseq("All") \ abortion_owners, aseq("Homeowners") \ homeless_owners, aseq("Homeowners")\  abortion_renters, aseq("Don't Own") \ homeless_renters, aseq("Don't Own") \abortion_prolife, aseq("Pro-Life") \ abortion_mixed, aseq("Mixed Attitudes") \ abortion_prochoice, aseq("Pro-Choice")), keep(N_homeless_trt N_abortion_trt ) xline(0, lcolor(black) lwidth(thin)) level(95) graphregion(fcolor(white) lcolor(none) ilcolor(none) color(white) lwidth(large)) xlabel(-.25(.25).75, labsize(small)) xscale(lw(none))  legend(off) ylabel(,labsize(small)) swap eqlabels("Homeless Shelters" "Out-of-State Abortion Services", asheadings) mcolor(black) msize(small)
graph export "Figure1.eps", as(eps) replace


**OUTPUT DIRECT EFFECTS REGRESSIONS (TABLE SM.2)
esttab abortion_pooled_reg homeless_pooled_reg using regressions.tex, replace nogaps compress booktabs wrap varwidth(45) b(%9.3f) se(%9.3f) star(* 0.05 ** .01) nonotes label title("Direct Effects Regressions"\label{regressions})  postfoot("\tabnotes{3}{Cell entries are OLS coefficients; robust standard errors in parentheses. Higher values on outcome correspond to ``more difficult.'' Because respondents could select multiple ethnoracial identities there is no reference group associated with that set of measures." "\sym{*} \(p<0.05\), \sym{**} \(p<.01\).}")

**OUTPUT INTERACTION REGRESSIONS (TABLE SM.3)
esttab abortion_renters_reg abortion_prolife_reg abortion_interact homeless_renters_reg homeless_interact using interactions.tex, replace nogaps compress booktabs wrap varwidth(45) b(%9.3f) se(%9.3f) star(* 0.05 ** .01) nonotes label title("Interaction Models"\label{interactions}) keep(TREAT race_* age educ income income_mis g_man g_other home_mortgage home_own home_other ab_mixed ab_prochoice mixedXtrt prochoiceXtrt TREATXHomeowner _cons) order(TREAT home_mortgage home_own home_other TREATXHomeowner ab_mixed ab_prochoice mixedXtrt prochoiceXtrt race_* age educ income income_mis g_man g_other _cons) nonotes postfoot("\tabnotes{6}{Cell entries are OLS coefficients; robust standard errors in parentheses. Higher values on outcome correspond to ``more difficult.'' Because respondents could select multiple ethnoracial identities there is no reference group associated with that set of measures." "\sym{*} \(p<0.05\), \sym{**} \(p<.01\).}") nomtitles mgroups("Out-of-State Abortion Services" "Homeless Shelters", pattern(1 0 0 1 0)) 



*****BUSINESS AND PARTISANSHIP INTERACTIONS (see Discussion section and SM document)***
//merge in Census data
merge m:1 zip using "..\employment_zip.dta"
drop if _merge!=3
drop _merge

//generate "folded" strength of PID variable
gen str_pid=abs(partyid-4)
label var str_pid "Strength of Party ID (0-3)"
//Interactions
foreach i in N_abortion_trt N_homeless_trt{
	gen XempX`i'=emp*`i'
	gen XretailX`i'=retail*`i'
	gen XhealthX`i'=health*`i'
	gen XpartyidX`i'=partyid*`i'
	gen Xstr_pidX`i'=str_pid*`i'
}
label var XempXN_abortion_trt "Neighborhood x Employees"
label var XretailXN_abortion_trt "Neighborhood x Retail"
label var XhealthXN_abortion_trt "Neighborhood x Health"
label var XpartyidXN_abortion_trt "Neighborhood x Party ID"
label var Xstr_pidXN_abortion_trt "Neighborhood x Strength PID"

label var XempXN_homeless_trt "Neighborhood x Employees"
label var XretailXN_homeless_trt "Neighborhood x Retail"
label var XhealthXN_homeless_trt "Neighborhood x Health"
label var XpartyidXN_homeless_trt "Neighborhood x Party ID"
label var Xstr_pidXN_homeless_trt "Neighborhood x Strength PID"

label var TREAT "Neighborhood"
gen XempX =.
gen XretailX = .
gen XhealthX = .
gen XpartyidX = .
gen Xstr_pidX = .
label var XempX "Neighborhood x Employees"
label var XretailX "Neighborhood x Retail"
label var XhealthX "Neighborhood x Health"
label var XpartyidX "Neighborhood x Party ID"
label var Xstr_pidX "Neighborhood x Strength PID"

//LOOP THROUGH ESTIMATING INTERACTIONS W/VARIOUS POTENTIAL MODERATORS
foreach i in emp retail health partyid str_pid{
replace X`i'X =X`i'XN_abortion_trt 
replace TREAT = N_abortion_trt 
reg NIMBYAbortion TREAT `i' X`i'X race_* age educ income income_mis g_man g_other home_mortgage home_own home_other ab_mixed ab_prochoice, r
estimates store abortion_`i'

replace X`i'X =X`i'XN_homeless_trt 
replace TREAT = N_homeless_trt 
reg NIMBYHomeless TREAT `i' X`i'X race_* age educ income income_mis g_man g_other home_mortgage home_own home_other, r
estimates store homeless_`i'
}

**OUTPUT TABLE SM.4
esttab abortion_emp abortion_retail abortion_health abortion_partyid abortion_str_pid using exploratory.tex, replace nogaps compress booktabs wrap varwidth(48) b(%9.3f) se(%9.3f) star(* 0.05 ** .01) nonotes keep(TREAT X*  emp retail health partyid str_pid) order(TREAT X* emp retail health partyid str_pid) label title("ZIP Code Business Information x Treatment Regressions"\label{exploratory})  postfoot("\tabnotes{5}{Cell entries are OLS coefficients; robust standard errors in parentheses. Higher values on outcome correspond to ``more difficult.'' All models control for respondent demographics; coefficients not reported for presentation purposes." "\sym{*} \(p<0.05\), \sym{**} \(p<.01\).}") 




/*****2022 CES; DATA DOWNLOADABLE FROM HARVARD DATAVERSE*** VERIFIES ABORTION ATTITUDES IN CCCS ARE SIMILAR TO CES
use "CCES2022_Common.dta", clear
keep if countyname=="Cook IL"
tab CC22_332a
tab CC22_332a CC22_332f
tab CC22_332a CC22_332f, cel
*/

